﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
/// <summary>
/// Class1 的摘要说明
/// </summary>



/// <summary>
/// JSON帮助类
/// </summary>
public static class JsonHelper
    {
    public static object JsonConvert { get; private set; }

    /// <summary>
    /// 格式化成Json字符串
    /// </summary>
    /// <param name="obj">需要格式化的对象</param>
    /// <returns>Json字符串</returns>
    //public static string ToJson(DataTable obj)
    //{
    //    JavaScriptSerializer serializer = new JavaScriptSerializer();
    //    return serializer.Serialize(obj);
    //}
    public static string ToJson(DataTable dt)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
        foreach (DataRow dr in dt.Rows)//每一行信息，新建一个Dictionary<string,object>,将该行的每列信息加入到字典
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            foreach (DataColumn dc in dt.Columns)
            {
                result.Add(dc.ColumnName, dr[dc].ToString());
            }
            list.Add(result);
        }
        string jsonstr = serializer.Serialize(list);

        return jsonstr;//调用Serializer方法 
    }
    /// <summary>   
    /// Datatable转换为Json   
    /// add yuangang by 2015-05-19
    /// </summary>   
    /// <param name="table">Datatable对象</param>   
    /// <returns>Json字符串</returns>   
    public static string ToJson1(DataTable dt)
    {
        StringBuilder jsonString = new StringBuilder();
        jsonString.Append("[");
        DataRowCollection drc = dt.Rows;
        for (int i = 0; i < drc.Count; i++)
        {
            jsonString.Append("{");
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                string strKey = dt.Columns[j].ColumnName;
                string strValue = drc[i][j].ToString();
                Type type = dt.Columns[j].DataType;
                jsonString.Append("\"" + strKey + "\":");
                strValue = String.Format(strValue, type);
                if (j < dt.Columns.Count - 1)
                {
                    jsonString.Append(strValue + ",");
                }
                else
                {
                    jsonString.Append(strValue);
                }
            }
            jsonString.Append("},");
        }
        jsonString.Remove(jsonString.Length - 1, 1);
        jsonString.Append("]");
        if (jsonString.Length == 1)
        {
            return "[]";
        }
        return jsonString.ToString();
    }


    public static string ToJson(List<string>T)
    {
        DataContractJsonSerializer json = new DataContractJsonSerializer(T.GetType());

        string szJson = "";

        //序列化

        using (MemoryStream stream = new MemoryStream())
        {

            json.WriteObject(stream, T);

            szJson = Encoding.UTF8.GetString(stream.ToArray());

        }
        return szJson;
    }



    public static void RegistAjax(HtmlHead Header, Type type)
    {
        string assemblyName = type.FullName + "," + type.Assembly.FullName.Substring(0, type.Assembly.FullName.IndexOf(","));
        if (type.Assembly.FullName.StartsWith("App_Code."))
        {
            assemblyName = type.FullName + ",App_Code";
        }
        Literal l = new Literal();
        l.Text = "\n<script type=\"text/javascript\" src=\"/ajaxpro/prototype.ashx\"></script>\n"
        + "<script type=\"text/javascript\" src=\"/ajaxpro/core.ashx\"></script>\n"
        + "<script type=\"text/javascript\" src=\"/ajaxpro/converter.ashx\"></script>\n"
        + "<script type=\"text/javascript\" src=\"/ajaxpro/" + assemblyName + ".ashx\"></script>\n";
        Header.Controls.Add(l);
    }
}  



